//
//  BCustomLoadingHUD.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/3.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCustomLoadingHUD.h"

@interface BCustomLoadingHUDView ()

@property (nonatomic, strong) CAShapeLayer *backCircleLayer;

/**
 *animateLayer
 */
@property (nonatomic, strong) CAShapeLayer *animateLayer;

@end

@implementation BCustomLoadingHUDView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubLayers];
    }
    return self;
}

- (void)setupSubLayers {
    self.backCircleLayer = [CAShapeLayer layer];
    self.backCircleLayer.fillColor = [UIColor clearColor].CGColor;
    self.backCircleLayer.lineWidth = 3;
    self.backCircleLayer.strokeColor = [UIColor separatorColor].CGColor;
    [self.layer addSublayer:self.backCircleLayer];
    
    //
    self.animateLayer = [CAShapeLayer layer];
    self.animateLayer.fillColor = [UIColor clearColor].CGColor;
    self.animateLayer.lineWidth = 3;
    self.animateLayer.lineCap = kCALineCapRound;
    self.animateLayer.strokeColor = [UIColor darkMainColor].CGColor;
    [self.backCircleLayer addSublayer:self.animateLayer];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.backCircleLayer.frame = self.bounds;
    self.animateLayer.frame = self.bounds;
    //
    [self updatePath];
}

- (void)updatePath {
    [self setBackgroundCircleLayer];
    //
    self.animateLayer.path = [self circleBezierPathWithAngle:M_PI*1.5].CGPath;
}

- (void)setBackgroundCircleLayer {
    self.backCircleLayer.path = [self circleBezierPathWithAngle:M_PI*1.5].CGPath;
}

- (UIBezierPath *)circleBezierPathWithAngle:(CGFloat)endAngle {
    CGPoint centerPoint = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
    return [UIBezierPath bezierPathWithArcCenter:centerPoint radius:self.frame.size.width/2 startAngle:-M_PI/2 endAngle:endAngle clockwise:true];
}


- (void)startLoading {
    CGFloat percent = 0.2;
    CGFloat strokeStart = (random()%9)/10.0;
    CGFloat strokeEnd = strokeStart+percent;
    self.animateLayer.strokeStart = strokeStart;
    self.animateLayer.strokeEnd = strokeEnd;
    //
    CABasicAnimation *transform = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    transform.fromValue = @(0);
    transform.toValue = @(M_PI*4);
    transform.duration = 2;
    transform.repeatCount = HUGE_VALF;
    transform.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transform.removedOnCompletion = false;
    [self.animateLayer addAnimation:transform forKey:@""];
    
    //
    CABasicAnimation *transform2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    transform2.fromValue = @(0);
    transform2.toValue = @(M_PI*2);
    transform2.duration = transform.duration*2.5;
    transform2.repeatCount = HUGE_VALF;
    transform2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    transform2.removedOnCompletion = false;
    [self.backCircleLayer addAnimation:transform2 forKey:@""];
}

- (void)stopLoading {
    [self.animateLayer removeAllAnimations];
    [self.backCircleLayer removeAllAnimations];
    //
    [self.animateLayer removeFromSuperlayer];
    [self.backCircleLayer removeFromSuperlayer];
}

@end


@interface BCustomLoadingHUD ()

@end

@implementation BCustomLoadingHUD

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
    }
    return self;
}
- (void)setupSubviews {
    _loadingView = [BCustomLoadingHUDView new];
    [self addSubview:_loadingView];
    [self.loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.size.mas_equalTo(CGSizeMake(30.0, 30.0));
        make.centerY.mas_equalTo(self).offset(-Height_Bottom_SafeArea);
    }];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    //
    [self.loadingView layoutSubviews];
}

@end
